package com.service.vod.service.impl;

import com.aliyun.vod.upload.UploadVideo;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.service.base.exception.MyException;
import com.service.vod.config.AliyunVodConfig;
import com.service.vod.service.VodService;
import com.service.vod.utils.AliyunVodSdkUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private AliyunVodConfig aliyunVodConfig;

    @Override
    public String getUrl(String id) {
        DefaultAcsClient client = null;
        String url = "";
        try {
            client = AliyunVodSdkUtil.initVodClient(aliyunVodConfig.getAccessKeyId(), aliyunVodConfig.getAccessKeySecret());
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(id);
            response = client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                url = playInfo.getPlayURL();
            }
        } catch (Exception e) {
            throw new MyException(20001, "获取视频地址失败！！");
        }
        return url;
    }

    @Override
    public Map<String, Object> uploadVod(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        //获取上传文件的文件名
        String filename = file.getOriginalFilename();
        map.put("filename", filename);
        //获取当前日期
        String date = new DateTime().toString("yyyy/MM/dd");
        //将当前日期 uuid 原始文件名 拼接起来组成新的文件名
        filename = date + "/" + UUID.randomUUID().toString().replaceAll("-", "") + "" + filename;
        String title = filename.substring(0, filename.lastIndexOf("."));
        String id = "";
        try {
            UploadStreamRequest request = new UploadStreamRequest(aliyunVodConfig.getAccessKeyId(), aliyunVodConfig.getAccessKeySecret(), title, filename, file.getInputStream());
            UploadStreamResponse response = new UploadVideoImpl().uploadStream(request);
            if (!response.isSuccess()) {
                throw new MyException(20001, "上传视频失败");
            }
            id = response.getVideoId();
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("id", id);
        return map;
    }

    @Override
    public void delVod(String id) {
        //删除上传到阿里云的视频
        try {
            //初始化对象
            DefaultAcsClient client = AliyunVodSdkUtil.initVodClient(aliyunVodConfig.getAccessKeyId(), aliyunVodConfig.getAccessKeySecret());
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        } catch (Exception e) {
            throw new MyException(20001, "删除视频失败");
        }

    }

    @Override
    public String getPlayerAuth(String id) {
        DefaultAcsClient client = null;
        String playerAuth = "";
        try {
            client = AliyunVodSdkUtil.initVodClient(aliyunVodConfig.getAccessKeyId(), aliyunVodConfig.getAccessKeySecret());
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            playerAuth = response.getPlayAuth();
        } catch (Exception e) {
            throw new MyException(20001, "获取视频播放凭证失败！！");
        }
        return playerAuth;
    }
}
